package edu.lingnan.house.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.lingnan.house.mapper.HouseMapper_lxj;
import edu.lingnan.house.pojo.TbHouse;
import edu.lingnan.house.service.HouseService_lxj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HouseServiceImpl_lxj implements HouseService_lxj {
    @Autowired
    private HouseMapper_lxj houseMapper;

    //房主上架房子
    @Override
    public int addHouse(TbHouse house) {
        if (house == null){
            System.out.println("提交的数据为空");
            return 0;
        }else {
            house.setState(1);
            int row = houseMapper.insert(house);
            return row;
        }
    }

    //房主查看房子 根据user_id
    @Override
    public List<TbHouse> findHouse(String userId) {
        QueryWrapper<TbHouse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<TbHouse> houses = houseMapper.selectList(queryWrapper);
        return houses;
    }

    //房主查看房子 根据user_id  state
    @Override
    public List<TbHouse> findHouse(String userId, Integer state) {
        QueryWrapper<TbHouse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("state",state);
        List<TbHouse> houses = houseMapper.selectList(queryWrapper);
        return houses;
    }

    //房主查看 申请上架中 的房子
    @Override
    public List<TbHouse> findHouse1(String userId) {
        QueryWrapper<TbHouse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("state",1);
        List<TbHouse> houses = houseMapper.selectList(queryWrapper);
        return houses;
    }

    //房主查看 上线中 的房子
    @Override
    public List<TbHouse> findHouse2(String userId) {
        QueryWrapper<TbHouse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("state",2);
        List<TbHouse> houses = houseMapper.selectList(queryWrapper);
        return houses;
    }

    //房主查看 下线 的房子
    @Override
    public List<TbHouse> findHouse3(String userId) {
        QueryWrapper<TbHouse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("state",3);
        List<TbHouse> houses = houseMapper.selectList(queryWrapper);
        return houses;
    }

    //房主查看 已出售并使用中 的房子
    @Override
    public List<TbHouse> findHouse4(String userId) {
        QueryWrapper<TbHouse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("state",4);
        List<TbHouse> houses = houseMapper.selectList(queryWrapper);
        return houses;
    }

    //房主查看 申请下架中 的房子
    @Override
    public List<TbHouse> findHouse5(String userId) {
        QueryWrapper<TbHouse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("state",5);
        List<TbHouse> houses = houseMapper.selectList(queryWrapper);
        return houses;
    }

    //房主申请下架房子
    @Override
    public int updateHouseToAgent(TbHouse house) {
        QueryWrapper<TbHouse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", house.getId());
        house.setState(5);
        int row = houseMapper.update(house, queryWrapper);
        return row;
    }

    //中介查询所负责的房子信息
    @Override
    public List<TbHouse> findHouseByAgentId(String agentId) {
        QueryWrapper<TbHouse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("agent_id",agentId);
        List<TbHouse> list = houseMapper.selectList(queryWrapper);
        return list;
    }

    //中介查询所负责的上架申请信息  agent_id  state取固定值
    @Override
    public List<TbHouse> findHouseByAgentIdAndSetState(String agentId) {
        QueryWrapper<TbHouse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("agent_id",agentId);
        queryWrapper.eq("state",1);
        List<TbHouse> list = houseMapper.selectList(queryWrapper);
        return list;
    }

    //中介查询所负责的下架申请信息  agent_id  state取固定值
    @Override
    public List<TbHouse> findHouseByAgentIdAndSetState3(String agentId) {
        QueryWrapper<TbHouse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("agent_id",agentId);
        queryWrapper.eq("state",5);
        List<TbHouse> list = houseMapper.selectList(queryWrapper);
        return list;
    }

    //中介审批通过房子上架申请
    @Override
    public int updateHouseApplyByAgent(TbHouse house) {
        if (house == null || house.equals("")){
            System.out.println("从浏览器得到的数据为空");
            return 0;
        }
        else {
            QueryWrapper<TbHouse> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", house.getId());
            house.setState(2);
            int row = houseMapper.update(house, queryWrapper);
            return row;
        }
    }

    //中介审批不通过房子上架申请
    @Override
    public int updateHouseApplyDownByAgent(TbHouse house) {
        if (house == null || house.equals("")){
            System.out.println("从浏览器得到的数据为空");
            return 0;
        }
        else {
            QueryWrapper<TbHouse> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", house.getId());
            house.setState(3);
            int row = houseMapper.update(house, queryWrapper);
            return row;
        }
    }

    //中介修改房子信息，如细节描述detail
    @Override
    public int updateHouseMessage(TbHouse house) {
        if (house == null || house.equals("")){
            System.out.println("从浏览器得到的数据为空");
            return 0;
        }
        else {
            QueryWrapper<TbHouse> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", house.getId());
            int row = houseMapper.update(house, queryWrapper);
            return row;
        }
    }
}
